<?php
/**
 * Geneone: Content Management Framework
 * Blog Object View
 *
 * http://geneone.neondragon.net/
 *
 * @package Geneone
 * @author Khlo
 * @version 1.0
 * @copyright 2004-2006 Geneone Development Team
 */
 
require_once ("Gene/Object/View.php");

class Gene_Object_View_Blog extends Gene_Object_View {
	/**
	 * Class Constructor
	 *
	 * @var object $model Gene_Object_Model object
	 */
	function Gene_Object_View_Blog(&$model) {
		$this->model =& $model;
	}
	
	/**
	 * Show this object. Returns HTML output code for object.
	 *
	 * @var array $actions Additional Arguments
	 *
	 * @return string HTML output for object.
	 */
	function view($actions=array()) {
		$tpl =& Gene::getTemplate();
		
		/* General Object Information */
		$info = $this->model->get();
		
		$tpl->assign ("pagetitle", $info['title']);
		$tpl->assign ("object", $info);
		
		/* Taxonomy */
		$set =& $this->model->getTaxonomySet();
		$tpl->assign ("categories", $set->getLabels());
		
		/* Parents */
		$parents = $this->model->getParents();
		$tpl->assign ("parents", $parents);
		
		/* Paging */
		$perpage = $this->model->getProperty('perPage');
		$this->model->enablePaging($perpage);
		$tpl->assign ("perpage", $perpage);
		
		if (isset($actions[2]) && isset($actions[3]) && $actions[2] == "tag") {
			$tag = str_replace("_", " ", $actions[3]);
			
			$numobjects = $set->getNumObjectsWithLabel($tag);
			$tpl->assign ("tagobjectcount", $numobjects);
			$numpages = ceil($numobjects / $perpage);
			$page = Gene_Request::getInteger("page", GENE_REQUEST_GET);
			if (!$page OR $page > $numpages) {
				$page = 1;
			}
			
			$this->model->setPageNumber($page);
			$tpl->assign ("pagenum", $page);
			$tpl->assign ("numpages", $numpages);
			
			$category = preg_replace('/\s\s+/', ' ', preg_replace("/[^a-zA-Z0-9 ]/", "", strtolower(trim($tag))));
			$tpl->assign("showingtag", $tag);
			
			$childs = $set->getObjectsByLabel($tag);
		} else {
			/* Get Blog Entries */
			$numpages = $this->model->getNumPages();
			$page = Gene_Request::getInteger("page", GENE_REQUEST_GET);
			if (!$page OR $page > $numpages) {
				$page = 1;
			}
			
			$this->model->setPageNumber($page);
			$tpl->assign ("pagenum", $page);
			$tpl->assign ("numpages", $this->model->getNumPages());
		
			$childs = $this->model->getChilds();
		}
		
		$tpl->assign ("childs", $childs);
		
		/* Latest Comments Portlet */
		$tpl->assign ("recentcomments", $this->model->getRecentComments());
		$tpl->append ("portlets", "blog");

		return $tpl->fetch("objects/blog/view");
	}
	
	/**
	 * Callback function to specify object specific edit actions.
	 */
	function getCustomEditActions() {
		if ($this->model->canModerate()) {
			$this->editActions['options'] = "Options";
		}
	}
	
	/**
	 * Callback function to call specific edit action
	 */
	function callCustomEditAction($action) {
		$tpl =& Gene::getTemplate();
		$db =& Gene::getDatabase();
		
		$info = $this->model->get();
		
		if ($action == "options" && $this->model->canModerate()) {
			if ($_SERVER['REQUEST_METHOD'] == "POST") {
				$this->model->setProperty("perPage", Gene_Request::getInteger("perpage", GENE_REQUEST_POST));
				$this->model->setProperty("pingServices", Gene_Request::getString("pingservices", GENE_REQUEST_POST));
				$this->model->setProperty("showCategories", Gene_Request::getBoolean("showcategories", GENE_REQUEST_POST) ? 1 : 0);
				$this->model->setProperty("showLatestComments", Gene_Request::getBoolean("showlatestcomments", GENE_REQUEST_POST) ? 1 : 0);
				$tpl->assign ("updated", true);
			}
			
			return $tpl->fetch ("objects/blog/edit/options");
		}
	}
	
	/**
	 * Displays the object creation form for this type of object.
	 *
	 * @static
	 *
	 * @return string HTML output for object creation.
	 */
	function create() {
		$tpl =& Gene::getTemplate();
		return $tpl->fetch("objects/blog/create");
	}
}
 
?>